package com.senney.practice.mutilThread.demo01;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

public class CallableAndFuture {
	public static class CallableTest implements Callable<String> {

		@Override
		public String call() throws Exception {
			return "Hello World!";
		}

	}

	public static void main(String[] args) {
		FutureTask<String> task = new FutureTask<>(new CallableTest());
		new Thread(task).start();
		try {
			System.out.println(task.get());
		} catch (Exception e) {
		}
	}

	public static void sumbitWithPool() {
		ExecutorService threadPool = Executors.newSingleThreadExecutor();
		Future<String> future = threadPool.submit(new CallableTest());
		try {
			System.out.println("waiting thread to finish");
			System.out.println(future.get());
		} catch (Exception e) {
			e.printStackTrace();
		}
		threadPool.shutdownNow();
	}

}
